
package com.yuanmei.model;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;

//import io.realm.annotations.Ignore;


public class TodayQuote extends RealmObject {
    private static final String TAG = TodayQuote.class.getSimpleName();
    @PrimaryKey
    private String necode;

    private String update;

    private float open, price, high, low, volume, turnover, percent;
    private float yestclose;

    private String buys, sells;

    public String getNecode() {
        return this.necode;
    }

    public void setNecode(String necode) {
        this.necode = necode;
    }

    public String getUpdate() {
        return this.update;
    }

    public void setUpdate(String update) {
        this.update = update;
    }

    public float getOpen() {
        return this.open;
    }

    public void setOpen(float open) {
        this.open = open;
    }

    public float getHigh() {
        return this.high;
    }

    public void setHigh(float high) {
        this.high = high;
    }

    public float getLow() {
        return this.low;
    }

    public void setLow(float low) {
        this.low = low;
    }

    public float getPrice() {
        return this.price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public float getVolume() {
        return this.volume;
    }

    public void setVolume(float volume) {
        this.volume = volume;
    }

    public float getTurnover() {
        return this.turnover;
    }

    public void setTurnover(float turnover) {
        this.turnover = turnover;
    }

    public float getPercent() {
        return this.percent;
    }

    public void setPercent(float percent) {
        this.percent = percent;
    }

    public float getYestclose() {
        return yestclose;
    }

    public void setYestclose(float yestclose) {
        this.yestclose = yestclose;
    }

    public String getBuys() {
        return buys;
    }

    public void setBuys(String buys) {
        this.buys = buys;
    }

    public String getSells() {
        return sells;
    }

    public void setSells(String sells) {
        this.sells = sells;
    }

    boolean isComplete() {
        return (this.price > 0.0f) && (this.volume > 0.0f) && (this.open > 0.0f)
                && (this.high > 0.0f) && (this.low > 0.0f);
    }

    public TodayQuote() {
        super();
    }

    public final static SimpleDateFormat sUpdateDateFormat
            = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.CHINA);
    public Date getUpdateDate(){
        try {
//            SimpleDateFormat updateFmt = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.CHINA);
            return sUpdateDateFormat.parse(getUpdate());
        } catch (ParseException e) {
            return null;
        }
    }

    public TodayQuote(String neCode, RealTimeQuote rtQuote) {
        this.necode = neCode;
        this.update = rtQuote.getUpdate();
        this.open = (float) rtQuote.getOpen();
        this.price = (float) rtQuote.getPrice();
        this.high =  (float) rtQuote.getHigh();
        this.low = (float) rtQuote.getLow();
        this.yestclose = (float) rtQuote.getYestclose();
        if (rtQuote.getVolume() != null) {
            this.volume = rtQuote.getVolume().floatValue();
        } else {
            Log.w(TAG, "create today quote volume is null neCode=" + neCode);
            this.volume = 0;
        }
        this.percent = (float) rtQuote.getPercent();
        // 这里的Turnover 不是换手，是交易额（元）
        this.turnover = (float) rtQuote.getTurnover();

        this.buys = String.format(Locale.CHINA,
                " 买1:%.2f %d\n 买2:%.2f %d\n 买3:%.2f %d\n 买4:%.2f %d\n 买5:%.2f %d",
                rtQuote.getBid1(), rtQuote.getBidvol1()/100,
                rtQuote.getBid2(), rtQuote.getBidvol2()/100,
                rtQuote.getBid3(), rtQuote.getBidvol3()/100,
                rtQuote.getBid4(), rtQuote.getBidvol4()/100,
                rtQuote.getBid5(), rtQuote.getBidvol5()/100
        );
        this.sells = String.format(Locale.CHINA,
                " 卖1:%.2f %d\n 卖2:%.2f %d\n 卖3:%.2f %d\n 卖4:%.2f %d\n 卖5:%.2f %d",
                rtQuote.getAsk1(), rtQuote.getAskvol1()/100,
                rtQuote.getAsk2(), rtQuote.getAskvol2()/100,
                rtQuote.getAsk3(), rtQuote.getAskvol3()/100,
                rtQuote.getAsk4(), rtQuote.getAskvol4()/100,
                rtQuote.getAsk5(), rtQuote.getAskvol5()/100
        );
    }

}
